package com.swlz.commons;

import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializeWriter;

/**
 * <pre>
 * FastJSON的相关配置
 * - 使用静态块配置，所以只需要在使用之前导入该类即可使用默认配置
 *  - long类型转换成字符串表示,默认:true
 *  - Date默认格式,默认yyyy-MM-dd
 * ClassName : com.swlz.commons.FastJSONConfig
 * Author : swlz
 * Date : 2020-06-30 10:25
 * Version : V1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes</pre>
 */
public class FastJSONConfig {

	private FastJSONConfig() {}
	private static FastJSONConfig config = new FastJSONConfig();

	static {
		config.initConfig();
	}
	
	public static FastJSONConfig getConfig() {
		return config;
	}
	
	private Boolean hasLongToString = true;
	private String defaultDateFormat = "yyyy-MM-dd";
	
	private ThreadLocal<SimpleDateFormat> dateFormat = ThreadLocal.withInitial(()->{
		return new SimpleDateFormat(defaultDateFormat);
	});
	
	
	private ObjectSerializer serializer = new ObjectSerializer() {
        @Override
        public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
                throws IOException {
            SerializeWriter out = serializer.getWriter();
            if(object==null) {
            	out.writeNull();
            	return;
            }
            if (fieldType == long.class || fieldType == Long.class || object instanceof Long) {
                if(hasLongToString==null || hasLongToString == true) {
                	out.writeString(Objects.toString(object, null));
                }else {
                	out.writeLong((Long)object);
                }
            }else if(fieldType == Date.class || object instanceof Date) {
            	out.writeString(dateFormat.get().format((Date)object));
            }
        }
    };
	
	public void initConfig() {
		SerializeConfig.globalInstance.put(Long.class, serializer);
		SerializeConfig.globalInstance.put(long.class, serializer);
		SerializeConfig.globalInstance.put(Date.class, serializer);
	}

	/**
	 * @param hasLongToString 是否将long类型转换为
	 * @return FastJSONConfig
	 */
	public FastJSONConfig setHasLongToString(Boolean hasLongToString) {
		this.hasLongToString = hasLongToString;
		return this;
	}

	/**
	 * @param defaultDateFormat 默认日期格式
	 * @return FastJSONConfig
	 */
	public FastJSONConfig setDefaultDateFormat(String defaultDateFormat) {
		this.defaultDateFormat = defaultDateFormat;
		 return this;
	}


	
}
